<script>
export default {
  name: 'loginSettings',
  data() {
    return {
      formData: {
        mobile: '',
        password: '',
        username: ''
      },
      formRules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }

        ],
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  methods: {
    /* 获取用户信息的action */
    async fetchUserInfo() {
      try {
        this.loading = true
        const userInfo = await this.$store.dispatch('employeesStore/fetchUserInfo', this.$route.params.id)
        this.formData = userInfo
      } catch (error) {
        this.$message.error(error.message)
      } finally {
        this.loading = false
      }
    },
    /* 点击更新按钮的回调函数 */
    async handleSubmit() {
      const valid = await this.$refs.elForm.validate().catch(error => error)
      if (!valid) return
      try {
        const message = await this.$store.dispatch('employeesStore/updateEmployessInfo', this.formData)
        this.$message.success(message)
        this.$router.back()
      } catch (error) {
        this.$message.error(error.message)
      }
    }
  },
  mounted() {
    this.fetchUserInfo()
  }
}
</script>

<template>
  <div class="loginSettings" v-loading="loading">
    <el-form label-width="100px" :model="formData" :rules="formRules" ref="elForm">
      <el-form-item label="姓名" prop="username">
        <el-input placeholder="请输入姓名" v-model="formData.username"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input placeholder="请输入手机号" v-model="formData.mobile"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input placeholder="请输入密码" type="password" v-model="formData.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" @click="$router.back()">取消</el-button>
        <el-button type="primary" @click="handleSubmit">更新</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped lang="scss">
.el-form {
  width: 25%;
}
</style>
